class CommentController < ApplicationController
layout :my_layout_func

before_filter :initvars
before_filter :dnsbl_check
before_filter :login_required, :only => [:edit,:update,:destroy,:list,:index,:show]

require 'net/http'

verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }	
	
  def index
    list
    render :action => 'list'
  end

  def list
    @comments = Comment.paginate :page => params[:page], :per_page => 20,:order=>"creation DESC"
  end

  def show
    @comment = Comment.find(params[:id])
  end

  def new
    create_oper
    if session[:loadedsettings]["comment_activation"]==1
      @comment = Comment.new
      @comment.media_id=Media.find_by_id(params[:id]).id
      render :action=>"new",:layout=>"default"
    else
      render :action=>"deactivated", :layout=>"default"
    end
  end

  def create
    @comment = Comment.new(params[:comment])
    @comment.creation=Time.now
    @comment.name=HTMLEntities.encode_entities(@comment.name,:named,:basic)
    @comment.comment=HTMLEntities.encode_entities(@comment.comment,:named,:basic)
    @comment.requester=request.env.to_s
    if session[:loadedsettings]["comment_moderation"]==1
      @comment.reviewed=0
      @comment.published=0
    else
      @comment.published=1
    end
    if check_operator
      if @comment.save
        expire_fragment(%r{list*})
        expire_fragment(%r{show/#{@comment.media_id}.*})
        expire_fragment(%r{lastcomments})
	      if session[:loadedsettings]["mail_notification"]==1
          CommentMailer.deliver_confirm(@comment,@settings)
        end
        redirect_to :controller => 'media', :action => 'show', :id => params[:comment][:media_id]
      else
        render :action => 'new',:id => params[:comment][:media_id]
      end
    else
      flash[:notice] = _('Spam, comment not saved! ')
      create_oper
      render :action => 'new'
    end
  end
  

  def edit
    @comment = Comment.find(params[:id])
  end

  def update
    @comment = Comment.find(params[:id])
    params[:comment]["name"]=HTMLEntities.encode_entities(params[:comment]["name"],:named,:basic)
    params[:comment]["comment"]=HTMLEntities.encode_entities(params[:comment]["comment"],:named,:basic)
    if @comment.update_attributes(params[:comment])
      expire_fragment(%r{list*})
      expire_fragment(%r{show/#{@comment.media_id}.*})
      expire_fragment(%r{lastcomments})
      #expire_action :controller=>"media",:action=>"show",:id=>@comment.media_id
      redirect_to :action => 'show', :id => @comment
    else
      render :action => 'edit'
    end
  end

  def destroy
    themedia=Comment.find(params[:id]).media_id
    Comment.find(params[:id]).destroy
    expire_fragment(%r{list*})
    expire_fragment(%r{show/#{themedia}.*})
    expire_fragment(%r{lastcomments})
    #expire_action :controller=>"media",:action=>"show",:id=>themedia
    redirect_to :action => 'list'
  end

  def deactivated
  end
  
   
  
protected

  def check_operator
    eval(session["oper3"].to_s+session["sign2"].to_s+session["oper4"].to_s)==params[:validation].to_s.to_i 
  end
  
  def create_oper
    session["oper3"]=0
    session["oper4"]=0
    while (eval(session["oper3"].to_s+session["sign2"].to_s+session["oper4"].to_s) == 0)
      session["oper3"]=rand(10)
      session["oper4"]=rand(10)
      @oper=["+","-","*"]
      session["sign2"]=@oper[rand(3)] 
    end
  end
  
  def my_layout_func
	['new','create'].include?(action_name)? 'default':'admin'
  end  

end
